﻿<SQLFunctionSpec
	xmlns="bb_appfx_sqlfunction"
	xmlns:common="bb_appfx_commontypes" 
	ID="32ef016e-e81c-4dd5-8668-0dad01b7a700"
	Name="USR_UFN_ISGUID"
	Description="Takes a string.  Returns 1 if the string is a valid guid.  0 if not."
	Author="Blackbaud Professional Services"
	DBFunctionName="USR_UFN_ISGUID"
	>

	<CreateFunctionSQL>
		<![CDATA[
CREATE FUNCTION dbo.USR_UFN_ISGUID(@strGUID  varchar(8000))
RETURNS bit
AS
BEGIN
  DECLARE  @result bit = 0;
  SELECT
    @strGUID = LTRIM(RTRIM(REPLACE(REPLACE(@strGUID, '{', ''), '}', '')))
   ,@strGUID = CASE WHEN LEN(@strGUID) = 32
                    THEN LEFT(@strGUID, 8) + '-' + SUBSTRING(@strGUID, 9, 4) + '-' + SUBSTRING(@strGUID, 13, 4) + '-' + SUBSTRING(@strGUID, 17, 4) + '-' + SUBSTRING(@strGUID, 21, 12) 
                    ELSE @strGUID
                    END
   ,@result = CASE WHEN @strGUID like '[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]' 
                   THEN 1
                   ELSE 0
                   END
  RETURN @result;
end;
]]>
	</CreateFunctionSQL>
</SQLFunctionSpec>